package com.itjeffrey.autocode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itjeffrey.autocode.bo.RoleInfoTbBO;
import com.itjeffrey.autocode.bo.req.IsEnableBO;
import com.itjeffrey.autocode.common.Result;
import com.itjeffrey.autocode.entity.RoleInfoTbEntity;
import com.itjeffrey.autocode.entity.RoleResEntity;
import com.itjeffrey.autocode.entity.UserRoleEntity;
import com.itjeffrey.autocode.mapper.RoleInfoTbMapper;
import com.itjeffrey.autocode.service.RoleInfoTbService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itjeffrey.autocode.util.DateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author: admin
 * @date: 2021-03-28 12:26:46
 */
@Service
public class RoleInfoTbServiceImpl extends ServiceImpl<RoleInfoTbMapper, RoleInfoTbEntity> implements RoleInfoTbService {

    @Resource
    private RoleInfoTbMapper roleInfoTbMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(RoleInfoTbBO roleInfoTbBO) {
        RoleInfoTbEntity roleInfoTbEntity = new RoleInfoTbEntity();
        BeanUtils.copyProperties(roleInfoTbBO, roleInfoTbEntity);
        roleInfoTbEntity.setCreateTime(DateUtil.getCurTime());
        roleInfoTbEntity.setEnabled(true);
        roleInfoTbMapper.insert(roleInfoTbEntity);
        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result mod(RoleInfoTbBO roleInfoTbBO) {
        RoleInfoTbEntity roleInfoTbEntity = new RoleInfoTbEntity();
        BeanUtils.copyProperties(roleInfoTbBO, roleInfoTbEntity);
        roleInfoTbEntity.setEnabled(true);
        roleInfoTbEntity.setUpdateTime(DateUtil.getCurTime());
        roleInfoTbMapper.update(roleInfoTbEntity,
                new QueryWrapper<RoleInfoTbEntity>().lambda().eq(RoleInfoTbEntity::getRoleName,
                        roleInfoTbBO.getRoleName()));
        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(String id) {
        roleInfoTbMapper.deleteById(id);
        return Result.ok();
    }

    @Override
    public Result queryList(RoleInfoTbBO roleInfoTbBO) {
        RoleInfoTbEntity roleInfoTbEntity = new RoleInfoTbEntity();
        BeanUtils.copyProperties(roleInfoTbBO, roleInfoTbEntity);
        LambdaQueryWrapper<RoleInfoTbEntity> queryWrapper = new QueryWrapper<RoleInfoTbEntity>().lambda();
        queryWrapper.like(StringUtils.isNotBlank(roleInfoTbBO.getRoleName()), RoleInfoTbEntity::getRoleName,
                roleInfoTbBO.getRoleName());
        queryWrapper.like(StringUtils.isNotBlank(roleInfoTbBO.getRoleDesc()), RoleInfoTbEntity::getRoleDesc,
                roleInfoTbBO.getRoleDesc());
        queryWrapper.orderByDesc(RoleInfoTbEntity::getRoleId);
        return Result.ok(roleInfoTbMapper.selectList(queryWrapper));
    }

    /**
     * 根据用户名查询角色信息
     *
     * @param username
     * @return
     */
    @Override
    @Cacheable(value = "AutoCode::RoleInfo", keyGenerator = "keyGenerator", unless = "#result==null")
    public List<RoleInfoTbEntity> queryRoleInfosByUsername(String username) {
        return roleInfoTbMapper.queryRoleInfosByUsername(username);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUserRole(UserRoleEntity userRoleEntity) {
        roleInfoTbMapper.addUserRole(userRoleEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addResRole(RoleResEntity roleResEntity) {
        roleInfoTbMapper.addResRole(roleResEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result isEnableRole(IsEnableBO isEnableBO) {
        roleInfoTbMapper.isEnableRole(isEnableBO);
        return Result.ok();
    }

    @Override
    public Result queryOne(Integer roleId) {
        return Result.ok(roleInfoTbMapper.selectById(roleId));
    }

    @Override
    public Result queryRolesOutOfSelfRole(RoleInfoTbBO roleInfoTbBO) {
        List<RoleInfoTbEntity> roleInfoTbEntities =
                roleInfoTbMapper.queryRolesOutOfSelfRole(roleInfoTbBO.getRoleName());
        List<String> roleNames = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(roleInfoTbEntities)){
            roleNames = roleInfoTbEntities.stream().map(t -> t.getRoleName()).collect(Collectors.toList());
        }
        return Result.ok(roleNames);
    }

    @Override
    public Result<List<RoleInfoTbEntity>> queryRoleInfosByLoginName(String loginName) {
        return Result.ok(roleInfoTbMapper.queryRoleInfosByLoginName(loginName));
    }

    public RoleInfoTbEntity queryRoleInfo(RoleInfoTbBO roleInfoTbBO) {
        RoleInfoTbEntity roleInfoTbEntity = new RoleInfoTbEntity();
        BeanUtils.copyProperties(roleInfoTbBO, roleInfoTbEntity);
        return roleInfoTbMapper.selectOne(new QueryWrapper<RoleInfoTbEntity>().setEntity(roleInfoTbEntity));
    }

}